package com.evangelion.jasper;


import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRBand;
import net.sf.jasperreports.engine.JRDataset;
import net.sf.jasperreports.engine.JRElement;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRPropertiesMap;
import net.sf.jasperreports.engine.JRPropertyExpression;
import net.sf.jasperreports.engine.JRSection;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.base.JRBaseComponentElement;
import net.sf.jasperreports.engine.base.JRBaseLine;
import net.sf.jasperreports.engine.base.JRBaseRectangle;
import net.sf.jasperreports.engine.base.JRBaseStaticText;
import net.sf.jasperreports.engine.base.JRBaseTextField;
import net.sf.jasperreports.engine.util.JRLoader;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

import com.evangelion.jasper.domain.Band;
import com.evangelion.jasper.domain.Color;
import com.evangelion.jasper.domain.ComponentElement;
import com.evangelion.jasper.domain.Element;
import com.evangelion.jasper.domain.Line;
import com.evangelion.jasper.domain.Rectangle;
import com.evangelion.jasper.domain.Report;
import com.evangelion.jasper.domain.StaticText;
import com.evangelion.jasper.domain.TextField;
import com.google.gson.Gson;
import net.sf.jasperreports.engine.base.JRBaseImage;


/**
 * Date: 2/12/14
 */
public class ReportReader {

    public static JasperReport readCompiledReport(String reportName) throws JRException {
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new File("D:\\WorkExt\\gisgert\\Evangelion\\web\\general\\reportes\\" + reportName + ".jasper"));
        System.out.println("name=" + jasperReport.getName());

        return jasperReport;
    }




    public static Report getReport(String filePath, Map<String, Element> changedElementMap) throws JRException {

        System.out.println("filePath=" + filePath);

        File file = new File(filePath);


        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(file);


        System.out.println("name=" + jasperReport.getName());

        JRDataset[] datasets = jasperReport.getDatasets();
        if (null != datasets) {
            for (JRDataset ds : datasets) {
                System.out.println("param: ");
                System.out.println("name=" + ds.getName());
                System.out.println("query: " + ds.getQuery());
            }
        }

        System.out.println("compiler class: " + jasperReport.getCompilerClass());

        System.out.println("allBands" + jasperReport.getAllBands());

        Report report = new Report();
        report.setUuid(jasperReport.getUUID());
        //report.setTitle(jasperReport.getTitle());


        if(null != jasperReport.getPageHeader()) {
            System.out.println("header height: " + jasperReport.getPageHeader().getHeight());
            report.setHeaderHeight(jasperReport.getPageHeader().getHeight());
        }

        if(null != jasperReport.getPageFooter()) {
            System.out.println("footer height: " + jasperReport.getPageFooter().getHeight());
            report.setFooterHeight(jasperReport.getPageFooter().getHeight());
        }

        report.setPageHeight(jasperReport.getPageHeight());
        report.setPageWidth(jasperReport.getPageWidth());


        report.setTopMargin(jasperReport.getTopMargin());
        report.setColumnSpacing(jasperReport.getColumnSpacing());
        report.setLeftMargin(jasperReport.getLeftMargin());
        report.setRightMargin(jasperReport.getRightMargin());
        report.setBottomMargin(jasperReport.getBottomMargin());



        JRSection detailSection = jasperReport.getDetailSection();
        JRBand[] bands = detailSection.getBands();

        List<Band> bandList = new ArrayList<Band>();
        report.setBands(bandList);

        if (null != bands) {
            for (JRBand band : bands) {
                System.out.println("-------- band ----");
                Band bb = new Band();

                JRElement[] elements = band.getElements();

                List<Element> elementList = new ArrayList<Element>();
                bandList.add(bb);

                if (null != elements) {

                    Map<String, String> elementMap = new HashMap<String, String>();
                    for (JRElement ele : elements) {

                        Element el = null;

                        System.out.println("");
                        System.out.println("-----------------");
                        System.out.println("element class is: " + ele.getClass());
                        elementMap.put(ele.getClass().getName(), ele.getClass().getName());
                        System.out.println("x=" + ele.getX());
                        System.out.println("y=" + ele.getY());


                        if(ele instanceof JRBaseStaticText) {
                            JRBaseStaticText staticText = (JRBaseStaticText)ele;
                            System.out.println("txt = " + staticText.getText());
                            el = new StaticText();
                            el.setText(staticText.getText());
                            //ele.setX(200);
                        }
                        else if(ele instanceof JRBaseRectangle) {
                            JRBaseRectangle rect = (JRBaseRectangle) ele;

                            el = new Rectangle();
                        }
                        else if (ele instanceof JRBaseLine) {
                            JRBaseLine line = (JRBaseLine) ele;
                            el = new Line();
                        }

                        else if (ele instanceof JRBaseComponentElement) {
                            JRBaseComponentElement comp = (JRBaseComponentElement) ele;
                            el = new ComponentElement();
                        }

                        else if (ele instanceof JRBaseTextField) {
                            JRBaseTextField textField = (JRBaseTextField) ele;
                            el = new TextField();
                        }
                        
                        else if (ele instanceof JRBaseImage) {
                            continue;
                        }
                        
                        System.out.println(ReflectionToStringBuilder.toString( ele));

                        if(ele.getUUID().toString().equals("d203a650-afa1-4ced-9aaf-444a14993758")) {

                            ele.getStyle();
                        }
                        if(ele.getUUID().toString().equals("a41698bf-3b8f-47da-975a-f2e0e1306775")) {

                            ele.getStyle();
                        }

                        el.setX(ele.getX());
                        el.setY(ele.getY());
                        el.setWidth(ele.getWidth());
                        el.setHeight(ele.getHeight());
                        //el.setUuid(ele.getUUID().toString());
                        el.setKey(ele.getKey());

                        if(null != ele.getOwnBackcolor()) {
                            java.awt.Color clr = ele.getOwnBackcolor();
                            Color color = new Color();
                            color.setBlue(clr.getBlue());
                            color.setRed(clr.getRed());
                            color.setGreen(clr.getGreen());
                            el.setOwnBackColor(color);
                        }

                        if(null != ele.getOwnForecolor()){
                            java.awt.Color clr = ele.getOwnForecolor();
                            Color color = new Color();
                            color.setBlue(clr.getBlue());
                            color.setRed(clr.getRed());
                            color.setGreen(clr.getGreen());
                            el.setOwnForeColor(color);
                        }


                        Element changedEle = changedElementMap.get(el.getKey());
                        if(null != changedEle && el.getType().equals("TEXT_FIELD")) {
                            el.setX(changedEle.getX());
                            el.setY(changedEle.getY());
                            el.setText(changedEle.getText());
                        }

                        elementList.add(el);
                        bb.setElements(elementList);

                        JRPropertiesMap propMap = ele.getPropertiesMap();
                        String[] names = propMap.getOwnPropertyNames();
                        if (null != names) {
                            for (String name : names) {
                                System.out.println("name=" + name);
                            }
                        }


                        JRPropertyExpression[] expressions = ele.getPropertyExpressions();

                        if (null != expressions) {
                            System.out.println("expressions -------");
                            for (JRPropertyExpression exp : expressions) {
                                exp.getName();
                            }
                        }

                    }
                    System.out.println("classes: ");
                    for(String key: elementMap.keySet()) {
                        System.out.println(key);
                    }
                }
            }
        }

        return report;
    }





    public static void main(String[] args) {
        String path = "E:\\projects\\spain_project\\Evangelion\\Evangelion\\web\\general\\reportes\\reporteMovimiento.jasper";
        Map<String, Element> cMap = new HashMap<String, Element>();
        try {
            Report report = ReportReader.getReport(path, cMap);
            Gson gson = new Gson();
            String json = gson.toJson(report);
            System.out.println(json);
        }
        catch (JRException e) {
            e.printStackTrace();
        }
    }
}
